package GUI.Ventas;

import java.rmi.RemoteException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import GUI.Ventas.Facturacion.CerrarVenta;
import employee.entities.EmployeeVO;
import sale.entities.SaleVO;
import sale.exceptions.EmptySaleExcpetion;
import sale.exceptions.NoSalesException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.employee.interfaces.EmployeeProxyMgt;
import service.sale.interfaces.SaleProxyMgt;

public class CerrarVentaMesa1 extends Dialog {

	protected Object result;
	protected Shell shell;
	int [] mesas;
	 public CerrarVentaMesa1(Shell parent){
	 super(parent);
	 setText("Seleccione ");
	 }
	 
	public CerrarVentaMesa1(Shell parent, int style) {
		super(parent, style);
		setText("Seleccione la Mesa y el Mozo");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	public Object open() throws RemoteException, ProxyException {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	private void createContents() throws RemoteException, ProxyException {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(428, 248);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		
		SaleProxyMgt aux = ServiceFacade.getInstance().getSaleProxyMgt();
		mesas = aux.getOpenTables();
		
		
		Label lblNumeroMesa = new Label(shell, SWT.NONE);
		FormData fd_lblNumeroMesa = new FormData();
		fd_lblNumeroMesa.top = new FormAttachment(0, 62);
		fd_lblNumeroMesa.left = new FormAttachment(0, 36);
		lblNumeroMesa.setLayoutData(fd_lblNumeroMesa);
		lblNumeroMesa.setText("Numero Mesa");
		
		Label lblMozo = new Label(shell, SWT.NONE);
		FormData fd_lblMozo = new FormData();
		fd_lblMozo.top = new FormAttachment(lblNumeroMesa, 25);
		fd_lblMozo.left = new FormAttachment(lblNumeroMesa, 0, SWT.LEFT);
		lblMozo.setLayoutData(fd_lblMozo);
		lblMozo.setText("Mozo");
		
		final Combo combo = new Combo(shell, SWT.NONE);
		FormData fd_combo = new FormData();
		fd_combo.right = new FormAttachment(100, -137);
		fd_combo.left = new FormAttachment(lblMozo, 73);
		fd_combo.top = new FormAttachment(lblMozo, -4, SWT.TOP);
		combo.setLayoutData(fd_combo);
		
		final Combo combo_1 = new Combo(shell, SWT.NONE);
		FormData fd_combo_1 = new FormData();
		fd_combo_1.right = new FormAttachment(combo, 0, SWT.RIGHT);
		fd_combo_1.bottom = new FormAttachment(combo, -13);
		fd_combo_1.left = new FormAttachment(lblNumeroMesa, 29);
		combo_1.setLayoutData(fd_combo_1);
		//combo.add("No especifica");
		for(int i=0;i<mesas.length;i++){
			if(mesas[i]>0){
			combo_1.add(String.valueOf(mesas[i]));
		}}
		
		EmployeeProxyMgt e = ServiceFacade.getInstance().getEmployeeProxyMgt();
		final EmployeeVO [] emp = e.getEmployees();
		for(int j=0; j<emp.length; j++){
			combo.add(emp[j].getName() + emp[j].getLastName());
		}
		
		
		
		Button btnAceptar = new Button(shell, SWT.NONE);
		btnAceptar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				
				
				int mesa = Integer.parseInt(combo_1.getText());
				String mozo = combo.getText();
				SaleProxyMgt uso = ServiceFacade.getInstance().getSaleProxyMgt();
				SaleVO[] sales = null;
				try {
					sales = uso.getOpenTableSales();
				} catch (RemoteException e2) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e2.printStackTrace();
				} catch (ProxyException e2) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e2.printStackTrace();
				} catch (NoSalesException e2) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "No hay Ventas Abiertas de Mesa");
					ventana.open();
					shell.dispose();
					e2.printStackTrace();
				}
				SaleVO venta = null;
				System.out.println(mesa);
				for(int i = 0; i< sales.length;i++){
					if (sales[i].getTableKey()==mesa){
						venta = sales[i];
						System.out.println("venta encontrada");
					}}
					EmployeeVO empleado = null;
					for(int j = 0;j< emp.length; j++){
						if ((emp[j].getName() + emp[j].getLastName()).equalsIgnoreCase(mozo)){
							empleado = emp[j];
							System.out.println("employee encotrado");
						}
					}
					venta.setEmp(empleado);
					CerrarVenta c = new CerrarVenta (shell, shell.getStyle(), venta, null);
					shell.setVisible(false);
					try {
						c.open();
					} catch (RemoteException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (EmptySaleExcpetion e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}}
				
				
			/*	CerrarVentaMesa2 cerrar = new CerrarVentaMesa2(shell,shell.getStyle(), mesa, mozo);
				try {
					cerrar.open();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ProxyException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}*/
		});
						
		FormData fd_btnAceptar = new FormData();
		fd_btnAceptar.top = new FormAttachment(lblMozo, 60);
		fd_btnAceptar.left = new FormAttachment(lblNumeroMesa, 0, SWT.LEFT);
		btnAceptar.setLayoutData(fd_btnAceptar);
		btnAceptar.setText("Aceptar");
	
	}

}
